import 'package:flutter/material.dart';
import 'package:common/global/style.dart';

class TVerticalNavigationBar extends StatefulWidget {
  final List<TNavigationItem> items;
  final Function(int currentIndex) onTabChanged;
  final int initialIndex;
  final double barWidth;
  final bool showIndicator;
  final bool orientationVertical;
  final double textSize;
  final double spaceSize;
  final double verticalHeight;

  TVerticalNavigationBar(
      {@required this.items,
      @required this.onTabChanged,
      this.initialIndex = 0,
      this.barWidth = 140,
      this.showIndicator = true,
      this.orientationVertical = true,
      this.textSize = 15,
      this.spaceSize = 10,
      this.verticalHeight = 90});

  @override
  State<StatefulWidget> createState() => _TNavigationState(initialIndex);
}

class _TNavigationState extends State<TVerticalNavigationBar> {
  int currentIndex;

  _TNavigationState(this.currentIndex);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: widget.barWidth == 0 ? null : widget.barWidth,
      child: Column(
        mainAxisAlignment: widget.verticalHeight == 0 ? MainAxisAlignment.spaceEvenly : MainAxisAlignment.start,
        crossAxisAlignment:
            !widget.orientationVertical && widget.barWidth == 0 ? CrossAxisAlignment.start : CrossAxisAlignment.center,
        children: widget.items
            .map((e) => GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {
                    final newIndex = widget.items.indexOf(e);
                    widget.onTabChanged(newIndex);
                    setState(() {
                      currentIndex = newIndex;
                    });
                  },
                  child: Container(
                    height: widget.verticalHeight == 0 ? null : widget.verticalHeight,
                    width: widget.barWidth == 0 ? null : widget.barWidth,
                    alignment: widget.orientationVertical ? Alignment.center : AlignmentDirectional.centerStart,
                    decoration: widget.showIndicator
                        ? BoxDecoration(
                            border: Border(
                                left: BorderSide(
                                    width: 3,
                                    color: widget.items.indexOf(e) == currentIndex
                                        ? TColor.primeColor
                                        : Colors.transparent)))
                        : null,
                    child: widget.orientationVertical
                        ? Column(
                            mainAxisSize: MainAxisSize.min,
                            children: getChildren(e),
                          )
                        : Row(
                            mainAxisSize: MainAxisSize.min,
                            children: getChildren(e),
                          ),
                  ),
                ))
            .toList(),
      ),
    );
  }

  List<Widget> getChildren(TNavigationItem e) {
    return [
      !(e.cornerMarker == null || e.cornerMarker == 0)
          ? Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                SizedBox(width: 15),
                widget.items.indexOf(e) == currentIndex ? e.iconSelected : e.iconNormal,
                Container(
                  alignment: Alignment.center,
                  height: 15,
                  width: 15,
                  margin: EdgeInsets.only(bottom: 15),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(300),
                    color: Colors.red,
                  ),
                  child: Text(
                    e.cornerMarker > 99 ? '99+' : '${e.cornerMarker}',
                    style: TextStyle(fontSize: e.cornerMarker > 99 ? 8 : 10, color: Colors.white),
                  ),
                ),
              ],
            )
          : widget.items.indexOf(e) == currentIndex
              ? e.iconSelected
              : e.iconNormal,
      SizedBox(
        height: widget.orientationVertical ? widget.spaceSize : 1,
        width: widget.orientationVertical ? 1 : widget.spaceSize,
      ),
      Text(
        e.text,
        textScaleFactor: 1,
        style: TextStyle(
            fontSize: widget.textSize,
            color: widget.items.indexOf(e) == currentIndex ? TColor.primeColor : Color(0xFF6F7275)),
      ),
    ];
  }
}

class TNavigationItem {
  final Widget iconSelected;
  final Widget iconNormal;
  final String text;
  final int id;
  final String code;
  final int cornerMarker; //角标
  TNavigationItem(
      {@required this.iconNormal,
      @required this.iconSelected,
      @required this.text,
      this.id,
      this.code,
      this.cornerMarker});
}
